
package com.cysyz.web.modules.base.user.controller;

import com.cysyz.web.modules.base.businesslog.annotation.BusinessLogInfo;
import com.cysyz.web.modules.base.inputvalidation.annotation.ValidateConfig;
import com.cysyz.web.modules.base.permission.annotation.Module;
import com.cysyz.web.modules.base.permission.annotation.ModuleFunction;
import com.cysyz.web.modules.base.permission.service.RoleService;
import com.cysyz.web.modules.base.permission.support.ModuleFunctionType;
import com.cysyz.web.modules.base.support.SysConstants;
import com.cysyz.web.modules.base.support.controller.SpringMVCBaseController;
import com.cysyz.web.modules.base.user.domain.SysUser;
import com.cysyz.web.modules.base.user.service.SysUserService;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;


@Controller
@RequestMapping("/sysuser")
@Module(name="账户管理", code="sysUser")
public class SysUserController extends SpringMVCBaseController {
    protected static final String DEFAULT_SORT_COLUMNS=null;

    public static final String PAGE_PATH_PREFIX = SysConstants.BACK_PAGE_DIR_PATH + "/sysUser";

    @Resource
    private SysUserService sysUserService;

    private final String LIST_ACTION="redirect:list.do";


    /** binder用于bean属性的设置 */
    @InitBinder
    public void initBinder(WebDataBinder binder){
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
    }



    /** 列表 */
    @RequestMapping("/list")
    @ValidateConfig
    @ModuleFunction(value = "账户列表", moduleFunctionType = ModuleFunctionType.MENU)
    public String list(){
        return this.PAGE_PATH_PREFIX + "/list";
    }

    /** 查询 */
    @RequestMapping("/search")
    @ResponseBody
    @ValidateConfig
    @ModuleFunction(value = "账户列表", menuPrivilegeMethodName = "list")
    public String search(SysUser sysUser){
                /*
                if (sysUser != null && sysUser.getName() != null) {
                    sysRole.setName(transcodingIfGet(sysRole.getName()));
                }
                */
        transcodingIfGet(sysUser, "name");

        return responseJSON(sysUserService.getSysUserList(sysUser));
    }

       /* *//** 显示 *//*
        @RequestMapping(value = "/{id}")
        public String show(ModelMap model,@PathVariable java.lang.Longid)throws Exception{
                SysUsersysUser=(SysUser)sysUserManager.getById(id);
                model.addAttribute("sysUser",sysUser);
                return"/sysuser/show";
                }*/

    @Resource
    private RoleService roleService;

    @RequestMapping("/addUI")
    public String addUI() {

        setRequestAttribute("roleList", roleService.getAllRoleList());

        return this.PAGE_PATH_PREFIX + "/add";
    }

    @RequestMapping("/add")
    @ModuleFunction("账户添加")
    @BusinessLogInfo(operationName = "用户添加")
    public String add(SysUser sysUser, @RequestParam("roleId") Long roleId) {
        sysUserService.addNewSysUser(sysUser, roleId);
        return this.LIST_ACTION;
    }

    @RequestMapping("/validateLoginName")
    @ResponseBody
    public String validateLoginNameExist(@RequestParam("loginName") String loginName) {
        return new Boolean(sysUserService.validateLoginNameExits(loginName)).toString();
    }

    @RequestMapping("/edit")
    @ModuleFunction("账户修改")
    @BusinessLogInfo(operationName = "账户修改")
    public String edit(SysUser sysUser, @RequestParam("roleId") Long roleId) {
        sysUserService.editSysUser(sysUser, roleId);
        return this.LIST_ACTION;
    }

    @RequestMapping("/editUI")
    public String editUI(Long id) {
        setRequestAttribute("roleList", roleService.getAllRoleList());
        setRequestAttribute("sysUser", sysUserService.getUser(id));
        return this.PAGE_PATH_PREFIX + "/edit";
    }

    /** 删除 *//*
        @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
        public String delete(ModelMap model,@PathVariable java.lang.Longid){
                sysUserManager.removeById(id);
                Flash.current().success(DELETE_SUCCESS);
                return LIST_ACTION;
                }

        *//** 批量删除 *//*
        @RequestMapping(method = RequestMethod.DELETE)
        public String batchDelete(ModelMap model,@RequestParam("items")java.lang.Long[]items){
                for(int i=0;i<items.length;i++){
                sysUserManager.removeById(items[i]);
                }
                Flash.current().success(DELETE_SUCCESS);
                return LIST_ACTION;
                }
*/
    @RequestMapping("/lock")
    @ResponseBody
    @ModuleFunction(value = "账户停用")
    @BusinessLogInfo(operationName = "账户停用")
    public String lock(@RequestParam("userId") Long userId) {
        sysUserService.lock(userId);
        return responseJSON();
    }

    @RequestMapping("/unLock")
    @ResponseBody
    @ModuleFunction(value = "账户恢复使用")
    @BusinessLogInfo(operationName = "账户恢复使用")
    public String unLock(@RequestParam("userId") Long userId) {
        sysUserService.unLock(userId);
        return responseJSON();
    }
}

